﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ViewModel;
using DataLayer;
namespace Service
{
    public class NewIssue
    {
        public static List<DropDownItem> GetPriorityOptions()
        {
            List<DropDownItem> priorityOptions = new List<DropDownItem>();
            try
            {
                using (iTrackerEntities DB = new iTrackerEntities())
                {
                    priorityOptions = DB.priorities.Select(x => new DropDownItem()
                    {
                        ID = x.id,
                        Name = x.name
                    }).ToList();
                }
            }
            catch (Exception ex)
            {
                Helper.ErrorLogger.WriteLog("GetPriorityOptions", ex);
                throw;
            }            
            return priorityOptions;
        }

        public static List<DropDownItem> GetStatusOptions()
        {
            List<DropDownItem> statusOptions = new List<DropDownItem>();
            try
            {
                using (iTrackerEntities DB = new iTrackerEntities())
                {
                    statusOptions = DB.status.Select(x => new DropDownItem()
                    {
                        ID = x.id,
                        Name = x.name
                    }).ToList();
                }
            }
            catch (Exception ex)
            {
                Helper.ErrorLogger.WriteLog("GetStatusOptions", ex);
                throw;
            }            
            return statusOptions;
        }

        public static int SaveIssueItem(Issue obj)
        {
            int chk = 0;
            try
            {
                using (iTrackerEntities DB = new iTrackerEntities())
                {
                    if (obj.ID > 0)
                    {
                        issue o = DB.issues.Where(x => x.id == obj.ID).FirstOrDefault();
                        o.name = obj.Name;
                        o.priority_id = (int)obj.Priority;
                        o.status_id = (int)obj.Status;
                        o.date_start = obj.DateStart;
                        o.date_end = obj.DateEnd;
                        //delete old assignment
                        List<assignment> oldIds = DB.assignments.Where(x => x.issue_id == obj.ID).ToList();
                        foreach (var item in oldIds)
                        {
                            item.is_active = false;
                            item.date_updated = DateTime.Now;
                        }

                        //add new assignment
                        assignment a = new assignment()
                        {
                            issue_id = obj.ID,
                            shop_id = obj.Assignee.FirstOrDefault().ID,
                            date_assigned = DateTime.Now,
                            is_active = true,
                            is_deleted = false
                        };
                        DB.assignments.Add(a);
                    }
                    else
                    {
                        //add new assignment
                        foreach (var item in obj.Assignee)
                        {
                            issue o = new issue()
                            {
                                name = obj.Name,
                                description = obj.Description,
                                priority_id = (int)obj.Priority,
                                status_id = (int)obj.Status,
                                date_created = DateTime.Now,
                                date_start = obj.DateStart,
                                date_end = obj.DateEnd,
                                is_request = obj.IsRequest,
                                udf1 = 0,
                                udf2 = "",
                                is_deleted = false
                            };
                            DB.issues.Add(o);
                            DB.SaveChanges();
                            assignment a = new assignment()
                            {
                                issue_id = o.id,
                                shop_id = item.ID,
                                date_assigned = DateTime.Now,
                                is_deleted = false,
                                is_active = true
                            };
                            DB.assignments.Add(a);

                            announcer ann = new announcer() {
                            issue_id=o.id,
                            datetime=DateTime.Now
                            };
                            DB.announcers.Add(ann);
                        }
                    }
                    chk = DB.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                Helper.ErrorLogger.WriteLog("SaveIssueItem", ex);
                throw;
            }
            
            return chk;
        }


    }
}
